﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace BIN
{
    public  class AutoUpdateLog
    {
        private StreamWriter _SW = null;
        public AutoUpdateLog(string FileName)
        {
            try
            {
                if (!(System.IO.File.Exists(FileName)))
                {
                    FileInfo fi = new FileInfo(FileName);
                    if (!(fi.Directory.Exists))
                        Directory.CreateDirectory(fi.Directory.ToString());
                }
                _SW = new StreamWriter(FileName, false); //删除以前的日志
                _SW.AutoFlush = true;
            }
            catch { }
        }
        public void Dispose()
        {
            _SW = null;
        }
        ~AutoUpdateLog()
        {
            this.Dispose();
        }
        public void AddLog(LOG_MODE LogMode, string LogText)
        {
            if (_SW == null) return;
            try
            {
                _SW.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString(),
                    LogMode == LOG_MODE.INFO ? "info" :
                    (LogMode == LOG_MODE.WARN ? "warn" : "error"), LogText));
            }
            catch { }
        }
        public static readonly AutoUpdateLog LogObj =
            new AutoUpdateLog(ShareMet.CombineDir(
                ShareMet.GetAppDir(), @"Logs\UpdateLog.log"));
    }
}
